﻿using System;
using System.Collections.Generic;
using System.Text;
using YouHP.BLL;
using YouHP.Utility;
using YouHP.Model;
using System.Text.RegularExpressions;
using System.Web;

namespace YouHP.Pages.User
{
    public class FriendApplyList : UserBasePage
    {
        protected override void ShowPage()
        {
            base.ShowPage();
            if (this.ErrorCount > 0) return;

            HandleCommon();
            if (IsPost) HandlePost();
            else HandleGet();
        }

        protected List<UserInfo> FriendDataList;
        protected int PageIndex;
        protected int DataCount;
        protected int PageCount;
        protected string PagingString = "";
        protected int State;

        protected void HandleCommon()
        {
            this.SetTitle("好友申请");
            this.AddLocation("好友申请", "");
            
        }

        protected void HandlePost()
        {
            int actiontype = RequestTools.GetFormInt("actiontype", 0);
            int[] idArray = Utils.StrToIntArray(RequestTools.GetFormString("itemid"), ",");
            if (idArray != null && idArray.Length > 0)
            {
                bool succ = false;
                if (actiontype == 1) // 删除好友
                {
                    succ = FriendBLL.Instance.DeleteFriend(UserData.Id, idArray);
                }
                else if (actiontype == 2) // 加为好友
                {
                    succ = FriendBLL.Instance.AcceptFriend(UserData.Id, idArray);

                    if (succ)
                    {

                        //添加动态
                        UserActionInfo Info = new UserActionInfo();
                        Info.ActionTime = DateTime.Now;
                        Info.UserId = UserData.Id;
                        Info.UserName = UserData.UserName;
                        Info.ActionType = Constants.ACTION_FRIEND;
                        for (int i = 0; i < idArray.Length; i++)
                        {
                            Info.ActionContent += idArray[i] + "@";
                        }

                        UserActionBLL.Instance.AddUserAction(Info);

                        for (int i = 0; i < idArray.Length; i++)
                        {
                            UserActionInfo Info2 = new UserActionInfo();
                            Info2.ActionTime = DateTime.Now;
                            Info2.UserId = idArray[i];
                            Info2.UserName = UserBLL.Instance.GetUserInfo(Info2.UserId).UserName;
                            Info2.ActionType = Constants.ACTION_FRIEND;
                            Info2.ActionContent = UserData.Id + "@";
                            UserActionBLL.Instance.AddUserAction(Info2);
                        }
                    }


                }
                else if (actiontype == 3) // 拒绝
                {
                    succ = FriendBLL.Instance.RejectFriennd(UserData.Id, idArray);
                }

                if (succ)
                {
                    this.AddMsgLine("操作成功");
                    this.SetUrl("/User/FriendApplyList.aspx?state=" + State);
                    this.SetMetaRefresh();
                }
                else
                {
                    this.AddMsgLine("操作失败");
                    this.SetUrl("/User/FriendList.aspx?state=" + State);
                    this.SetMetaRefresh();
                }
            }
        }

        protected void HandleGet()
        {
            PageIndex = RequestTools.GetQueryInt("page", 1);
           
            FriendDataList = FriendBLL.Instance.GetFriendList(20, PageIndex, UserData.Id,Constants.FriendApplying, out DataCount);
            PageCount = Utils.GetPageCount(DataCount, 20);

            string query = HttpContext.Current.Request.Url.Query;
            query = Utils.RemoveQueryItem(query, "page");

            PagingString = this.GetPagingString(
                PageIndex,
                PageCount,
                "/User/FriendList.aspx" + query, "page");
        }
    }
}